#include "GenEEGData.h"


GenEEGData::GenEEGData(uint8_t channalNum){
    dataHead.channalNum = channalNum;
    // this->channalNum = channalNum;
};

void GenEEGData::clear(){
    // memset(&dataHead, 0, )
    data.clear();
    
}

GenEEGData::~GenEEGData(){

};

void GenEEGData::print(QString log){
    if(data.empty() || dataHead.channalNum == 0){printf("vector is empty.\n");return;}
    std::cout << log.toStdString() << std::endl; // 输出日志
    // 打印数据，竖着显示每个通道的值
    for (size_t i = 0; i < dataHead.channalNum; ++i) { // 遍历每个通道的数据(64)
        std::cout << "[" << i << "]:";
        for (size_t j = 0; j < data.size(); ++j) { // 遍历不同的通道
            if(i < data[j].size()) {
                std::cout << data[j][i] << " "; // 输出每个通道的数据
            }
        }
        std::cout << std::endl; // 换行，开始打印下一行数据
    }
}

// int inserData(const GenEEGData& inputData){
//     if(inputData.channalNum != this->channalNum){
//         std::cerr << "Inconsistent number of channals!" << std::endl;
//         std::cerr << "orin channals number:" << static_cast<int>(this->channalNum) 
//                     << " new channals number:" << static_cast<int>(inputData.channalNum) << std::endl;
//         return 0;
//     }

//     // 预留足够的空间以减少内存重新分配
//     // data.reserve(data.size() + inputData.data.size());
//     // data.insert(data.end(), inputData.data.begin(), inputData.data.end());

//     for(auto& it : inputData.data){
//         data.push_back(it);
//     }

//     return inputData.channalNum;        
// }

// GenEEGData& operator= (const GenEEGData& other){
//     if(&other == this){
//         return *this;
//     }

//     // data.clear();
//     // data.resize(other.data.size()); // 重新调整大小
//     // channalNum = other.channalNum;
//     // std::copy(other.data.begin(), other.data.end(), data.begin());
//     channalNum = other.channalNum;
//     data = other.data;  // 使用 vector 自带的赋值操作符

//     return *this;
// }

// bool operator == (const GenEEGData& other){
//     return channalNum == other.channalNum;
// }

int GenEEGData::saveData(const char* filePath, bool saveHead_flag){
    // 二进制写入
    static bool firstWrite = true;
    // FILE* file = fopen(filePath, "ab");
    FILE* file = fopen(filePath, "rb+");

    // 文件不存在时创建文件
    if (file == nullptr) {
        file = fopen(filePath, "wb+");
        if (!file) {
            std::cerr << "Failed to open or create file:" << filePath << std::endl;
            return false;
        }
        std::cout << "creat file:" << filePath << std::endl;
        
    }
    // std::cout << "sizeof(FileHeadStr)=" << sizeof(FileHeadStr) << std::endl;

    if( firstWrite || saveHead_flag ){
        // 文件头信息
        // double ch = eegData.dataHead.channalNum;
        // 使用 fseek 将指针移到文件开头读取内容
        // fseek(file, 0, SEEK_SET);
        int res = fwrite(&dataHead, sizeof(FileHeadStr), 1, file);
        if(res != 1){
            std::cerr << "Failed to write data!" << std::endl;
            fclose(file);
            return false;
        }
        fseek(file, 0, SEEK_END);
        #ifdef DEBUG
        std::cout << "eegData.channalNum = " << (int)eegData.dataHead.channalNum << std::endl;
        #endif // DEBUG
        firstWrite = false;
    }

    // rb+打开时指针默认在文件开头,指针修改为文件末尾
    fseek(file, 0, SEEK_END);

    // std::cout << "size of data = " << eegData.data.size() << std::endl;
    // 遍历vector，写入每个EEGDataArr的内容
    for (const auto& arr : data) {
        // 将每个数组的所有元素写入文件
        int res = fwrite(arr.data(), sizeof(double), MAX_CHANNALNUM, file);
        if(res != MAX_CHANNALNUM){
            std::cerr << "Error: Only " << res << " elements written, expected " << MAX_CHANNALNUM << std::endl;
            fclose(file);
            return false;
        }
    }

    fclose(file);
    return true;
}


bool GenEEGData::loadData(const char* filePath){
    FILE* file = fopen(filePath, "rb");
     if (file == nullptr) {
        std::cerr << "Failed to open file:" << filePath << std::endl;
        return false;
    }

    // 读取通道数量
    fread(&dataHead, sizeof(FileHeadStr), 1, file);
    EEGDataArr buffer;
    int count = 0;
    while (fread(buffer.data(), sizeof(double), MAX_CHANNALNUM, file) == MAX_CHANNALNUM){
        data.push_back(buffer);
        ++count;
    }

    fclose(file);
    std::cout << "read over, read len=" << count*MAX_CHANNALNUM*sizeof(double) << " Byte" << std::endl;
    return true;
}
